package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import DataObjects.Bon;
import DataObjects.MetaDataOrder;
import DataObjects.Payment;
import Messages.MessageAddNewPaymentOfTable;
import Messages.MessageCheckBalancePresentCard;
import Messages.MessageCheckBalancePresentCardReply;
import Messages.MessageCheckExistPresentCard;
import Messages.MessageCheckExistPresentCardReply;
import Messages.MessageGetFiresByOrderidReply;
import Messages.MessageSavingReply;

public class VKForPointOfSalePresentCard extends JPanel implements ActionListener {
	
	
	private JTextField txt;
	private Color btnColor;
	private Dimension screenSize;
	private int x;
	private int y;
	private MainMenu mainMenu;
	private JTextField txtF;
	
	private PfPointOfSale salePoint;
	private Vector<Bon> bon;
	
	private Float totalPrice;
	
	private MetaDataOrder metaDataOrder;
	
	private Float balance;
	

	public VKForPointOfSalePresentCard ()
	{
		super();
		setLayout(new GridLayout(4, 3,10,10));
        initialize();
    }
	
	public VKForPointOfSalePresentCard (Color c,final PfPointOfSale salePoint,final Vector<Bon> bon,Float totalPrice,MainMenu mm ,MetaDataOrder metaDataOrder )
	{
		super();
		
		this.mainMenu=mm;
		this.btnColor=c;
		this.salePoint=salePoint;
		this.bon=bon;
		this.totalPrice=totalPrice;
		this.metaDataOrder=metaDataOrder;
		setLayout(new GridLayout(4, 3,10,10));
        initialize();
    }
	
	
	private void initialize()
	{
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		for(int i = 1; i <= 9; i++) 
        	createButton(Integer.toString(i));
        createCheckButton();
        createNullButton();
        createBSpaceButton();
	}

	public JButton createCheckButton()
	{
		JButton checkButt=new JButton("בדוק יתרה");
		if(btnColor!=null)
        {
			checkButt.setBackground(btnColor);
        }
		checkButt.setFocusable(false);
		checkButt.setFocusPainted(false);
		checkButt.setPreferredSize(new Dimension(100, 100));
        Font font = checkButt.getFont();
        float size = font.getSize() + 13.0f;
        checkButt.setFont(font.deriveFont(size));
        add(checkButt);
	//    checkButt.setBounds(420, 15*y/23, 6*x/45, 2*y/24);
		
	    checkButt.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
              try{
					
					Integer f=Integer.parseInt(txtF.getText());
											
			    	}catch(NumberFormatException e1)
				{
					
					JOptionPane.showMessageDialog(null,"סכום לא תקין");
					txtF.setText("");
					 return;	
					
				}
				try{
				MessageCheckExistPresentCard msgExist = new MessageCheckExistPresentCard(Integer.parseInt(txtF.getText()));
					 mainMenu.ms.client.sendToServer(msgExist);
				MessageCheckExistPresentCardReply result = (MessageCheckExistPresentCardReply)mainMenu.ms.client.GetMessage();	
					 
						if (result.getCheckFlag()==0)
				    	{
							
				    		JOptionPane.showMessageDialog(null,"כרטיס לא קיים או יש בעיה בתקשורת ");
				    		return;
				    	}
				    }
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				try {
					  MessageCheckBalancePresentCard msgBalance = new MessageCheckBalancePresentCard(Integer.parseInt(txtF.getText()));
					  	  mainMenu.ms.client.sendToServer(msgBalance);
					  MessageCheckBalancePresentCardReply result = (MessageCheckBalancePresentCardReply)mainMenu.ms.client.GetMessage();	
						  balance=result.getBalance();
						  JOptionPane.showMessageDialog(null,"יתרה על  כרטיס:"+balance);
						  
					  }
					  catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				/*salePoint.remove(rightSecondPresentPanel);
				salePoint.add(getRightPanel(salePoint,bon));
				salePoint.repaint();*/

			}
		});
		return checkButt;
	}
	
	
	
	
    private void createButton(String label)
    {
        JButton btn = new JButton(label);
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        
        btn.setFocusable(false);
        btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
    }
    
    private void createNullButton()
    {
        JButton nullBtn = new JButton("0");
        if(btnColor!=null)
        {
        	nullBtn.setBackground(btnColor);
        }
        nullBtn.setFocusable(false);
        nullBtn.addActionListener(this);
        nullBtn.setFocusPainted(false);
        nullBtn.setPreferredSize(new Dimension(100, 100));
        Font font = nullBtn.getFont();
        float size = font.getSize() + 15.0f;
        nullBtn.setFont(font.deriveFont(size));
        add(nullBtn);
    }
    
    private void createBSpaceButton	()
    {
    	JButton bSpaceBtn=new JButton("BSpace");
    	bSpaceBtn.setFocusable(false);
    	if(btnColor!=null)
        {
    		bSpaceBtn.setBackground(btnColor);
        }
    	bSpaceBtn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		        JTextField txtF=(JTextField)component;
		        int caret=txtF.getCaretPosition();
		        String text=txtF.getText();	
		        
		        txtF.setText(text.substring(0,caret-1) + text.substring(caret));
		        txtF.setCaretPosition(caret-1);
			}
		});
    	
    	bSpaceBtn.setPreferredSize(new Dimension(100, 100));
        Font font = bSpaceBtn.getFont();
        float size = font.getSize()+x/140;
        bSpaceBtn.setFont(font.deriveFont(size));
        add(bSpaceBtn);
    }
    

    @Override
    public void actionPerformed(ActionEvent e)
    {
    	String actionCommand = e.getActionCommand();
        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        txtF=(JTextField)component;
        int caret=txtF.getCaretPosition();
        String text=txtF.getText();
        
                   
        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
        txtF.setCaretPosition(caret+txtF.getText().length());
    }
	
    
    public JTextField getTxt() {
		return txt;
	}

	public void setTxt(JTextField txt) {
		this.txt = txt;
	}
	
	public void setBtnColor(Color c)
	{
		this.btnColor=c;
	}
	

}

